<?php
namespace app\car\model;
use app\BaseModel;
use app\dynamic\model\CardUser;
use app\dynamic\model\UserInfo;
use app\shop\model\AdminCompany;
use think\facade\Db;



class ArticleList extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_motor_article_list';


    protected $append = [

        'user_id',

        'label'

    ];


    /**
     * @param $value
     * @param $data
     * @功能说明:标签
     * @author chenniang
     * @DataTime: 2021-11-23 14:56
     */
    public function getLabelAttr($value,$data){

        if(isset($data['id'])){

            $lable_text_model = new ArticleLabelText();

            $dis = [

                'a.article_id' => $data['id']
            ];

            $list = $lable_text_model->alias('a')
                    ->join('longbing_card_motor_article_label b','b.id = a.label_id')
                    ->where($dis)
                    ->field('b.id,b.title')
                    ->select()
                    ->toArray();

            return array_values($list);

        }

    }




    /**
     * @author chenniang
     * @DataTime: 2021-11-19 10:32
     * @功能说明:创建人
     */

    public function getUserIdAttr($value,$data){

        if(isset($data['id'])&&isset($data['is_public'])&&isset($data['create_user'])&&isset($data['uniacid'])){

            if($data['is_public']==0){

                $article_user_model = new ArticleUser();

                $list = $article_user_model->where(['article_id'=>$data['id']])->column('user_id');

            }else{
                //全选
                if($data['create_user']==1){
                    //公司
                    $company_model = new AdminCompany();

                    $dis = [

                        'uniacid' => $data['uniacid'],

                        'status'  => 1,

                        'pid'     => 0
                    ];

                    $list = $company_model->where($dis)->column('id');

                }else{
                    //员工
                    $user_model = new UserInfo();

                    $dis = [

                        'is_staff' => 1,

                        'uniacid'  => $data['uniacid']
                    ];

                    $list = $user_model->where($dis)->column('fans_id');

                }

            }

            return array_values($list);

        }

    }


    /**
     * @param $dis
     * @return array
     * 获取company
     */

    public function getId($data){

        $company_id = [];

        if(!empty($data)){

            foreach ($data as $v){

                $company_id[] = $this->reset($v);
            }

        }

        return array_values($company_id);
    }


    /**
     * @param $id
     * 回显
     */
    public function reset($id,&$data=array()){

        $cate   = Db::name('longbing_card_company')->where(['id'=>$id])->find();

        if(!empty($cate)&&$cate['pid']!=$cate['id']){

            array_unshift($data,$cate['id']);

            $this->reset($cate['pid'],$data);

        }

        return $data;
    }

    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:00
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        if(isset($data['user_id'])){

            $user = $data['user_id'];

            unset($data['user_id']);

        }

        if(isset($data['label'])){

            $label = $data['label'];

            unset($data['label']);

        }

        $res = $this->insert($data);

        $id  = $this->getLastInsID();

        if(isset($user)&&isset($label)){

            $this->updateSome($id,$user,$data['uniacid'],$data['create_user'],$label);
        }

        return $id;

    }


    /**
     * User: chenniang
     * Date: 2019-09-25 15:29
     * @return void
     * descption:转换插入数据
     */
    public function changeData($ids){

        $data = [];

        if(is_array($ids)){

            foreach ($ids as $k =>$v){

                $data[] = array_pop($v);

            }

        }

        return $data;
    }

    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:03
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        if(isset($data['user_id'])){

            $user = $data['user_id'];

            unset($data['user_id']);

        }

        if(isset($data['label'])){

            $label = $data['label'];

            unset($data['label']);

        }

        $res = $this->where($dis)->update($data);

        if(isset($user)&&isset($label)){

            $this->updateSome($data['id'],$user,$data['uniacid'],$data['create_user'],$label);
        }

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:04
     * @功能说明:详情
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:05
     * @功能说明:
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }



    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:05
     * @功能说明:
     */
    public function indexDataList($dis,$day,$sort,$where,$page=10){


//        dump($dis,$day);exit;
        $data = $this->alias('a')
                ->join('longbing_card_motor_article_user b','a.id = b.article_id AND b.type=2','left')
                ->join('longbing_card_motor_article_user c','a.id = c.article_id AND c.type=1','left')
                ->where($dis)
                ->where(function ($query) use ($where) {
                    $query->whereOr($where);
                })
                ->whereTime('a.create_time','>',"$day days")
                ->field('a.*')
                ->group('a.id')
                ->order($sort)
                ->paginate($page)
                ->toArray();

//        $data = $this->where($dis)->whereTime('create_time','>',"$day days")->order($sort)->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-02 13:59
     * @功能说明:获取员工的公司
     */
    public function getUserTopCompany($staff_id){

        $company_model = new AdminCompany();

        $user_model    = new \app\card\model\UserInfo();

        $company_id    = $user_model->where(['fans_id'=>$staff_id])->value('company_id');

        $top = $company_model->where(['id'=>$company_id])->value('top_id');

        return !empty($top)?$top:$company_id;

    }

    /**
     * @author chenniang
     * @DataTime: 2021-11-18 18:00
     * @功能说明:添加创建人
     *
     */
    public function updateSome($id,$data,$uniacid,$type,$label){

        $car_user_model = new ArticleUser();

        $car_user_model->where(['article_id'=>$id])->delete();

        if(!empty($data)){

            foreach ($data as $k=>$v){

                $insert[$k]= [

                    'uniacid'    => $uniacid,

                    'type'       => $type,

                    'article_id' => $id,

                    'user_id'    => $v
                ];

            }

            $car_user_model->saveAll($insert);

        }

        $lable_text_model = new ArticleLabelText();

        $lable_model      = new ArticleLabel();

        $lable_text_model->where(['article_id'=>$id])->delete();

        if(!empty($label)){

            foreach ($label as $value){

                $dis = [

                    'uniacid' => $uniacid,

                    'title'   => $value
                ];

                $find = $lable_model->dataInfo($dis);

                if(empty($find)){

                    $lable_model->dataAdd($dis);

                    $label_id = $lable_model->getLastInsID();

                }else{

                    $label_id = $find['id'];

                }

                $insert = [

                    'uniacid' => $uniacid,

                    'article_id' => $id,

                    'label_id' => $label_id
                ];

                $lable_text_model->insert($insert);

            }

        }

        return true;
    }




}